package com.apexsoft.aas.service.grpc;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class BooleanValue implements GrpcType, ObjectDeserializer, ObjectSerializer {
    private static final String type = "type.googleapis.com/google.protobuf.BoolValue";

    public static final BooleanValue instance = new BooleanValue();

    @Override
    public String type() {
        return type;
    }

    @Override
    public Boolean deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        Map<String, Boolean> data = (Map<String, Boolean>) parser.parseObject(new HashMap<String, Boolean>(1));
        return data.get("value");
    }

    @Override
    public int getFastMatchToken() {
        return JSONToken.UNDEFINED;
    }

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        SerializeWriter out = serializer.out;
        JSONObject data = new JSONObject();
        data.put("@type", type());
        data.put("value", object);
        out.write(data.toJSONString());

    }
}
